<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/quick_message.php';
require_once 'dbConnection.php';

class Quick_MessageDao {
	private $_db;
	
	public function __construct() {
		$this->_db = new DbConnection();
	}
	
	public function addQuick_Message($quick_message) {
		$query = sprintf("CALL sp_AddQuickMessage('%s', '%d', '%d', '%d', @QuickMessageId)", 
			$quick_message->getContent(), 
			$quick_message->getSenderId(), 
			$quick_message->getReceiverId(),
			$quick_message->getCreatedDate());
			
		$result = $this->_db->executeQuery($query);
		$result = $this->_db->executeQuery("SELECT @QuickMessageId");
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return 0;
		}
		
		if ($row = mysql_fetch_assoc($result)) {						
			$id = $row['@QuickMessageId'];			
		}		
		
		return $id;
	}
	
	public function getQuick_MessagesBySenderId($senderId) {
		$query = sprintf("SELECT * FROM quick_messages WHERE SenderId = %d ORDER BY DESC", 
					mysql_escape_string($senderId));
		
		$result = $this->_db->executeQuery($query);
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return 0;	
		}
		$qm = array();
		
		while ($row = mysql_fetch_assoc($result)) {
			$quick_message = new Quick_MessageModel();
			
			$quick_message->setQuickMessageId($row['QuickMessageId']);
			$quick_message->setContent($row['Content']);
			$quick_message->setReceiverId($row['ReceiverId']);
			$quick_message->setSenderId($row['SenderId']);
			$quick_message->setCreatedDate($row['CreatedDate']);
			
			$qm[] = $quick_message;
		}		
		mysql_free_result($result);
		
		return $qm;
	}
	
	public function getQuick_MessagesByReceiverId($receiverId, $from, $to) {
		$query = sprintf("SELECT * FROM quick_messages WHERE ReceiverId = '%u' ORDER BY QuickMessageId DESC LIMIT %d, %d", 
					$receiverId, $from, $to);		
		
		$result = $this->_db->executeQuery($query);
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		$qm = array();
			
		while ($row = mysql_fetch_assoc($result)) {
			$qm[] = $this->setQMByAssoc($row);
		}
		
		mysql_free_result($result);
		
		return $qm;
	}
	
	public function removeQuickMessage($messageID) {
		global $ubi;
		
		$query = sprintf("CALL sp_DeleteQuickMessage('%d')", $messageID);
		
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return false;
		}
				
		return true;
	}
	
	public function countQuickMessage($receiverID) {
		$query = sprintf("SELECT COUNT(*) FROM quick_messages WHERE ReceiverId = '%u'", $receiverID);
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		if ($row = mysql_fetch_row($result)) {				
			$nQuickMessage = $row[0];
			return $nQuickMessage;
		}		
	}
	
	private function setQMByAssoc($row) {
		$qm = new Quick_MessageModel();
		$qm->setContent($row['Content']);
		$qm->setCreatedDate($row['CreatedDate']);
		$qm->setQuickMessageID($row['QuickMessageId']);
		$qm->setReceiverID($row['ReceiverId']);
		$qm->setSenderID($row['SenderId']);
		
		return $qm;
	}
}
?>